package com.mvc.web.user.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.web.common.md5Util.MD5Util;
import com.mvc.web.common.utils.OSSManageUtil;
import com.mvc.web.common.utils.PropertiesUtil;
import com.mvc.web.common.utils.StringUtils;
import com.mvc.web.main.service.MainService;
import com.mvc.web.main.vo.KeyValueVo;
import com.mvc.web.user.service.UserService;
import com.mvc.web.user.vo.UserVo;

/**   
* @Description: 
* @author 白海安 （baihaian@126.com） 2016年4月29日 下午2:51:53
* @version V1.0   
*/
@Controller
@RequestMapping(value = "/userController/*")
public class UserController {

	private static final String REGIST_INDEX = "regist/regist";

	@Autowired
	private UserService userService;
	@Autowired
	private MainService mainService;

	/**   
	* @Description: 注册跳转
	* @author 白海安 （baihaian@126.com） 2016年5月17日 上午10:15:37
	* @version V1.0   
	*/
	@RequestMapping("toRegist")
	public ModelAndView toRegist(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView(REGIST_INDEX);
		// 获取省份、城市
		List<KeyValueVo> province = this.mainService.getProvinceList();
		mav.addObject("province", province);
		return mav;
	}

	/**   
	* @Description: 注册
	* @author 白海安 （baihaian@126.com） 2016年5月17日 上午10:15:29
	* @version V1.0   
	*/
	@RequestMapping("regist")
	@ResponseBody
	public Map<String, String> regist(HttpServletRequest request,UserVo vo, MultipartFile image) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(StringUtils.isEmpty(vo.getPassword())||StringUtils.isEmpty(vo.getSubPassword())){
				map.put("error_code", "100");
				map.put("error_msg", "密码或确认密码不能为空");
				return map;
			}else if(!vo.getPassword().equals(vo.getSubPassword())){
				map.put("error_code", "101");
				map.put("error_msg", "密码或确认密码不一致");
				return map;
			}
			if (image == null || image.isEmpty()) {
				map.put("error_code", "102");
				map.put("error_msg", "首图不能为空");
				return map;
			}
			String fileName = image.getOriginalFilename();
			String fixName = fileName.substring(fileName.lastIndexOf(".") + 1,
					fileName.length());
			// 校验图片的真实性
			boolean vf = ValidateFixName(fixName);
			if(!vf){
				map.put("error_code", "103");
				map.put("error_msg", "首图格式不正确");
				return map;
			}
			String objectName = UUID.randomUUID().toString()+"."+fixName;
			String basePath = PropertiesUtil.getProperty("first_image_tmp")+objectName;
			image.transferTo(new File(basePath));
			OSSManageUtil.uploadFile(new File(basePath));
			vo.setFirstImage(objectName);
			vo.setPassword(MD5Util.string2MD5(vo.getPassword()));
			//计算年龄
			Integer age = StringUtils.getAgeFromBirthday(vo.getBirth());
			vo.setAge(age);
			Integer uid = this.userService.doRegist(vo);
			UserVo user = this.userService.getUserById(uid);
			request.getSession().setAttribute("user", user);
			map.put("error_code", "0");
			map.put("error_msg", "");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("error_code", "500");
			map.put("error_msg", "内部错误");
		}
		return map;
	}

	private boolean ValidateFixName(String FilenameExtension) {
		if (FilenameExtension.equals("JPEG")
				|| FilenameExtension.equals("jpeg")
				|| FilenameExtension.equals("JPG")
				|| FilenameExtension.equals("jpg")
				|| FilenameExtension.equals("PNG")
				|| FilenameExtension.equals("png")
				|| FilenameExtension.equals("GIF")
				|| FilenameExtension.equals("gif")
				|| FilenameExtension.equals("BMP")
				|| FilenameExtension.equals("bmp")) {
			return true;
		}
		return false;
	}
	
}
